﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZQNB.BaseLib.Core;
using ZQNB.BaseLib.Core.Models;
using ZQNB.Common.Caching;
using ZQNB.Common.Ioc;

namespace ZQNB.Web.Common.Contexts
{
    public interface INbSiteContextProvider :
        ISiteHealthStatusService,
        IProductInfoService,
        IAceSettingService,
        IBsSettingService,
        INavMenuService
    {
    }

    public class NbSiteContext : INbSiteContextProvider
    {
        public static INbSiteContextProvider Current
        {
            get
            {
                return CoreServiceProvider.LocateService<INbSiteContextProvider>();
            }
        }

        private readonly IPerApplicationCacheManager _perApplicationCacheManager;
        private readonly CacheHelper _cacheHelper;
        private readonly ISiteHealthStatusService _siteHealthStatusService;
        private readonly IProductInfoService _productInfoService;
        private readonly IAceSettingService _aceSettingService;
        private readonly IBsSettingService _bsSettingService;
        private readonly INavMenuService _navMenuService;

        public NbSiteContext(
            IPerApplicationCacheManager perApplicationCacheManager
            , CacheHelper cacheHelper
            , ISiteHealthStatusService siteHealthStatusService
            , IProductInfoService productInfoService
            , IAceSettingService aceSettingService
            , IBsSettingService bsSettingService
            , INavMenuService navMenuService)
        {
            _perApplicationCacheManager = perApplicationCacheManager;
            _cacheHelper = cacheHelper;
            _siteHealthStatusService = siteHealthStatusService;
            _productInfoService = productInfoService;
            _aceSettingService = aceSettingService;
            _bsSettingService = bsSettingService;
            _navMenuService = navMenuService;
        }

        public ProductInfo GetProductInfo()
        {
            string cacheKey = _cacheHelper.MakeCacheKey<ProductInfo>();
            var data = _perApplicationCacheManager.Get<ProductInfo>(cacheKey);
            if (data == null)
            {
                data = _productInfoService.GetProductInfo();
                _perApplicationCacheManager.Set(cacheKey, data);
            }

            return data;
        }

        public void SaveProductInfo(ProductInfo model)
        {
            _productInfoService.SaveProductInfo(model);
            //remove cache
            string cacheKey = _cacheHelper.MakeCacheKey<ProductInfo>();
            _perApplicationCacheManager.Remove(cacheKey);
        }

        public AceSetting GetAceSetting()
        {
            string cacheKey = _cacheHelper.MakeCacheKey<AceSetting>();
            var data = _perApplicationCacheManager.Get<AceSetting>(cacheKey);
            if (data == null)
            {
                data = _aceSettingService.GetAceSetting();
                _perApplicationCacheManager.Set(cacheKey, data);
            }

            return data;
        }

        public void SaveAceSetting(AceSetting model)
        {
            _aceSettingService.SaveAceSetting(model);
            //remove cache
            string cacheKey = _cacheHelper.MakeCacheKey<AceSetting>();
            _perApplicationCacheManager.Remove(cacheKey);
        }

        public IList<SiteHealthStatus> GetAllSiteHealthStatuses()
        {
            //no cache
            var data = _siteHealthStatusService.GetAllSiteHealthStatuses();
            return data;
        }

        public SiteHealthStatus GetCurrentSiteHealthStatus()
        {
            string cacheKey = _cacheHelper.MakeCacheKey<SiteHealthStatus>();
            var data = _perApplicationCacheManager.Get<SiteHealthStatus>(cacheKey);
            if (data == null)
            {
                data = _siteHealthStatusService.GetCurrentSiteHealthStatus();
                _perApplicationCacheManager.Set(cacheKey, data);
            }

            return data;
        }

        public void ApplicationStart()
        {
            _siteHealthStatusService.ApplicationStart();
        }

        public void ApplicationEnd()
        {
            _siteHealthStatusService.ApplicationEnd();
        }

        public BsSetting GetBsSetting()
        {
            string cacheKey = _cacheHelper.MakeCacheKey<BsSetting>();
            var data = _perApplicationCacheManager.Get<BsSetting>(cacheKey);
            if (data == null)
            {
                data = _bsSettingService.GetBsSetting();
                _perApplicationCacheManager.Set(cacheKey, data);
            }

            return data;
        }

        public void SaveBsSetting(BsSetting model)
        {
            _bsSettingService.SaveBsSetting(model);
            //remove cache
            string cacheKey = _cacheHelper.MakeCacheKey<BsSetting>();
            _perApplicationCacheManager.Remove(cacheKey);
        }

        public void Init(IList<NavMenu> models, bool replaceExist = false)
        {
            _navMenuService.Init(models, replaceExist);
            //remove cache
            string cacheKey = _cacheHelper.MakeCacheKey<IList<NavMenu>>();
            _perApplicationCacheManager.Remove(cacheKey);
        }

        public void Validate(IList<NavMenu> models)
        {
            _navMenuService.Validate(models);
        }

        public IList<NavMenu> GetNavMenus()
        {
            string cacheKey = _cacheHelper.MakeCacheKey<IList<NavMenu>>();
            var data = _perApplicationCacheManager.Get<IList<NavMenu>>(cacheKey);
            if (data == null)
            {
                data = _navMenuService.GetNavMenus();
                _perApplicationCacheManager.Set(cacheKey, data);
            }

            return data;
        }
    }
}
